package com.example.viewdemo.fragment

import android.annotation.SuppressLint
import android.content.Context
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.example.viewdemo.R
import com.example.viewdemo.fragment.LocaleHelper.applyLocale
import java.util.Locale

class FragmentTest : AppCompatActivity() {
    private lateinit var testBtn: Button
    private lateinit var changeLanguageBtn: Button
    private lateinit var changeLanguageTv: TextView
    private var isEnglish = true
    @SuppressLint("MissingInflatedId")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()


        setContentView(R.layout.activity_fragment_test)
        testBtn =findViewById<Button>(R.id.left_fragmentBtn)
        changeLanguageBtn =findViewById<Button>(R.id.change_language_btn)
        changeLanguageTv =findViewById(R.id.change_language_tv)

        changeLanguageBtn.setOnClickListener {
            changeLanguage()
        }

//        testBtn.setOnClickListener{
//            replaceFragment(AnotherRightFragment())
//        }
//        replaceFragment(RightFragment())

        testBtn.setOnClickListener {
            // 短按事件
            replaceFragment(AnotherRightFragment())
            Toast.makeText(this, "短按选 AnotherRightFragment", Toast.LENGTH_SHORT).show()
        }
        testBtn.setOnLongClickListener {
            // 长按事件
            replaceFragment(RightFragment())
            Toast.makeText(this, "长按选 ARightFragment", Toast.LENGTH_SHORT).show()
            true // 返回 true 表示消费了长按事件，不再触发点击事件
        }



    }
    private fun readLanguage() {
        // 读取并应用保存的语言设置
        val savedLocale = getSharedPreferences("AppSettings", Context.MODE_PRIVATE).getString("language", "en")
        applyLocale(this, Locale(savedLocale))
        updateLanguageDisplay()
    }

    private fun changeLanguage() {
        isEnglish = !isEnglish  // 切换语言标志
        val locale = if (isEnglish) Locale.ENGLISH else Locale.CHINESE
        applyLocale(this, locale)  // 应用新的语言环境
        saveLanguagePreference(locale)  // 保存语言设置
        updateLanguageDisplay()  // 更新显示当前语言
    }

    private fun saveLanguagePreference(locale: Locale) {
        val editor = getSharedPreferences("AppSettings", Context.MODE_PRIVATE).edit()
        editor.putString("language", locale.language).apply()
    }


    private fun updateLanguageDisplay() {
        val languageText = if (isEnglish) "Current Language: English" else "当前语言: 中文"
        changeLanguageTv.text = languageText
    }


    private fun replaceFragment(fragment: Fragment) {
        val fragmentManager = supportFragmentManager
        val transaction = fragmentManager.beginTransaction()
        transaction.replace(R.id.rightLayout, fragment)
        transaction.addToBackStack(null)
        transaction.commit()
    }
}

object LocaleHelper {
    fun applyLocale(context: Context, locale: Locale) {
        val resources = context.resources
        val configuration = resources.configuration
        configuration.setLocale(locale)
        resources.updateConfiguration(configuration, resources.displayMetrics)
    }
}